from market.view import View
from market.mathView import view2vec
from market.models import Asset, Player
from market.models import Strategy, AssetPercentageInStrategy, Fund
from market.math import isARepartition




###########################################################################################
# check that we have a repartition over a set of standard assets or 100 percent of an asset with a fixed value
def isStrategyValid(strat):
	assetList = strat.assetpercentageinstrategy_set.all()
	if len(assetList) == 0:
                return [False,'no asset in the strategy '  + strat.name ]
	if len(assetList) == 1:
                if assetList[0].asset.isAvailableForStandardStrategy:
                        tmp = isARepartition( [assetList[0].assetPercentageInStrategy] )
                        return [ tmp[0], strat.name + " " + tmp[1]]
                else:
                        return [assetList[0].asset.fixValue > 0, "fixed strat " + strat.name + " should have a positive value" ]
        else:
                rep = []
                for a in assetList:
                        if not( a.asset.isAvailableForStandardStrategy ):
                                return [False,"asset "+ a.asset.name +" not available for strategy " + strat.name ]
                        rep.append( a.assetPercentageInStrategy )
                tmp = isARepartition( rep )
                return [ tmp[0], strat.name + " " + tmp[1]]

                
                        
       
###########################################################################################
# return a couple. the first element is boolean describing  if we have 100% of a single fixed asset. A fixed strat is necessarily valid.
# the second is the value when fixed, a random number when not fixed
def isFixedStrat(strat):
	assetList = strat.assetpercentageinstrategy_set.all()
	if len(assetList) == 1:
                if assetList[0].asset.isAvailableForStandardStrategy:
                        return [False,-1]
                else:
                        return [assetList[0].asset.fixValue > 0,assetList[0].asset.fixValue]
        return [False,-1]



###########################################################################################
# return the bank account as the only fiwed strat. If there are no fixed strat or several, it return nothing and fill the message status
def getBankAccount(betroom):
        stratList = Strategy.objects.filter(betroom=betroom)
        fixStratList = []
        for s in stratList:
                if isFixedStrat(s)[0]:
                     fixStratList.append(s)
        if len(fixStratList) == 1:
                return fixStratList[0]
        if len(fixStratList) > 1:
                raise Exception("there should be one and only one fixed strategy which is the bank account")
        if len(fixStratList) == 0:
                raise Exception("there is no bank acount")
                


###########################################################################################
# return the mtm of the fund associated with the strat in argument
def getFundMtM(player, marketData, strat):
        try:
                f = Fund.objects.get( player =  player, strategy = strat )
        except:
                return 0
        return f.numberOfAssetsInFund * marketData.getStrategyMtM(strat)


###########################################################################################
# return the mtm of the portfolio (composed of different strategies and number of assets associated to them)
def getPortfolioMtM(player, marketData):
        mtm = 0
        for f in Fund.objects.filter( player = player ):
                tmp = f.numberOfAssetsInFund
                mtm = mtm + tmp * marketData.getStrategyMtM( f.strategy )
        return mtm



###########################################################################################
# returns the percentage of the specified asset in the specified strategy
def getPercOnAssetInStrat(strat, asset):
	assetPerc = AssetPercentageInStrategy.objects.filter( strategy = strat, asset =  asset )
	if len( assetPerc ) == 0:
                return 0
        if len( assetPerc ) == 1:
                return assetPerc[0].assetPercentageInStrategy
        raise Exception( "something went wrong in strategy "+self.dbPlayer.mainStrategy.name )



###########################################################################################
# retuns the number of the specified asset in the specified fund
def getNumberOfSpecifiedAssetInFund(fund, asset):
        return fund.numberOfAssetsInFund * getPercOnAssetInStrat(fund.strategy, asset)



